#include "RegCodeGenerator.h"
#include "DefineCommon.h"
// #include <QtCore/QFile>
#include <iostream>
#include <fstream>


RegCodeGenerator::RegCodeGenerator()
{
}

RegCodeGenerator::~RegCodeGenerator()
{
}

void RegCodeGenerator::wrapClssListToFiles(EStringList classList, const EString& dirName)
{
    {
        EString fileName = dirName + "/RegFunctionOsg_gen.h";

        std::ofstream file(fileName.data());
        if(!file)
        {
            ELOG_ERROR("Failed to open file %s", fileName.data());
            return;
        }

        EString content;
        this->genRegFun_h(classList, content);
        file<<content.data()<<std::endl;

        file.close();
    }

    {
        EString fileName = dirName + "/RegFunctionOsg_gen.cpp";

        std::ofstream file(fileName.data());
        if(!file)
        {
            ELOG_ERROR("Failed to open file %s", fileName.data());
            return;
        }

        EString content;
        this->genRegFun_cpp(classList, content);
        file<<content.data()<<std::endl;

        file.close();
    }
}

void RegCodeGenerator::genRegFun_h(EStringList classList, EString& content)
{
    content += "#ifndef REG_FUNCTION_OSG_GEN_H\n";
    content += "#define REG_FUNCTION_OSG_GEN_H\n";
    content += "\n";
    content += "#include <DefineLibUiWrapOsg.h>\n";
    content += "#include <QtCore/EString>\n";
    content += "#include <QtCore/EStringList>\n";
    content += "#include <QtWidgets/QWidget>\n";
    content += "#include <ItemBase.h>\n";
    content += "\n";
    content += "\n";
    content += "namespace RegFunctionOsg\n";
    content += "{\n";
    content += "    UI_WRAP_OSG_DLL EStringList getItemTypeList();\n";
    content += "    UI_WRAP_OSG_DLL ItemBasePtr newItemBase(const EString& typeName);\n";
    content += "}\n";
    content += "#endif // RegFunction\n";
}

void RegCodeGenerator::genRegFun_cpp(EStringList classList, EString& content)
{
    content += "#include <iostream>\n";
    content += "#include <RegFunctionOsg_gen.h>\n";
    content += "\n";

    for(unsigned int i = 0 ; i < classList.size(); i++)
    {
        // CppInfo::ClassPtr c = classList[i];
        // if(c->userData["constructType"] == "nosimpleConstruct")
        // {
            // continue;
        // }
        EString c = classList[i];
        content += EString("#include <")+c+".h>\n";
    }


    content += "\n";
    content += "EStringList RegFunctionOsg::getItemTypeList()\n";
    content += "{\n";
    content += "    EStringList typeList;\n";
    content += "    typeList\n";

    for(unsigned int i = 0 ; i < classList.size(); i++)
    {
        // CppInfo::ClassPtr c = classList[i];
        // if(c->userData["constructType"] == "nosimpleConstruct")
        // {
            // continue;
        // }
        // EString origClassName = c->userData["origClassName"];
        EString c = classList[i];
        EString origClassName=c;
        content += EString("    <<\"")+origClassName+"\"\n";
    }

    content += "        ;\n";
    content += "    return typeList;\n";
    content += "}\n";
    content += "\n";

    content += "ItemBasePtr RegFunctionOsg::newItemBase(const EString& typeName)\n";
    content += "{\n";
    content += "    ItemBasePtr newPtr;\n";
    content += "    if(typeName.isEmpty())\n";
    content += "    {\n";
    content += "    }\n";

    for(unsigned int i = 0 ; i < classList.size(); i++)
    {
        // CppInfo::ClassPtr c = classList[i];
        // if(c->userData["constructType"] == "nosimpleConstruct")
        // {
            // continue;
        // }
        // EString origClassName = c->userData["origClassName"];
        EString c = classList[i];
        EString origClassName=c;
    content += EString("    else if(typeName == \"") + origClassName+"\")\n";
    content += "    {\n";
    content += EString("        newPtr = std::make_shared<")+c+">();\n";
    content += "    }\n";
    }

    content += "\n";
    content += "    return newPtr;\n";
    content += "}\n";
}

